const app = getApp();
Page({
  data: {
    displayMode: 1, // 显示屏
    columnNum: 1, // 列数
    rowNum: 1,
    screenImg: app.getImgUrl("screen1.webp"),

    cameraIcon: app.getImgUrl("camera.png"),
    placeholderImg: app.getImgUrl("placeholder.png"),

    // 屏幕列表
    screenList: [],

    // 编辑屏幕弹窗
    isEdit: false,
    // 显示添加屏幕弹窗
    addScreenDialogShow: false,
    // actionSheet
    actionSheetShow: false,
    actionSheetShow2: false,
    // 预设弹窗
    presetsDialogShow: false,

    // 显示屏串号
    screenSN: "",
    // 显示屏别名
    screenAlias: "",

    actions: [
      { name: "编辑", index: 0, className: "actionsheet-item" },
      {
        name: "置顶",
        disabled: false,
        index: 1,
        className: "actionsheet-item",
      },
      {
        name: "删除",
        disabled: false,
        color: "#ee0a24",
        index: 2,
        className: "actionsheet-item",
      },
    ],

    actions2: [
      { name: "修改", index: 0, className: "actionsheet-item" },
      {
        name: "添加显示屏",
        disabled: false,
        index: 1,
        className: "actionsheet-item",
      },
      {
        name: "删除",
        disabled: false,
        color: "#ee0a24",
        index: 2,
        className: "actionsheet-item",
      },
    ],

    currentTab: "all",
    searchKeyword: "",

    selectedDeviceId: 1,
    deviceList: [
      {
        name: "语文百货-UI设备",
        id: 1,
      },
      {
        name: "语文百货-UI设备2",
        id: 2,
      },
      {
        name: "语文百货-UI设备3",
        id: 3,
      },
      {
        name: "语文百货-UI设备4",
        id: 4,
      },
    ],

    // 预设模板列表
    presetList: [
      {
        id: 1,
        name: "模板1",
      },
      {
        id: 2,
        name: "模板1",
      },
      {
        id: 3,
        name: "模板1",
      },
      {
        id: 4,
        name: "模板1",
      },
    ],
  },

  /**
   * 返回
   */
  onBack() {
    wx.navigateBack();
  },

  /**
   * 预设弹窗中tab切换
   * @param e
   */
  onTabChange(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentTab: tab,
    });
  },

  displayModeTapHandler(e) {
    const mode = e.currentTarget.dataset.mode + "";
    this.setData({
      displayMode: parseInt(mode),
    });
    if (mode === "1") {
      this.setData({
        columnNum: 1,
        rowNum: 1,
      });
    } else if (mode === "4") {
      this.setData({
        columnNum: 2,
        rowNum: 2,
      });
    } else if (mode === "9") {
      this.setData({
        columnNum: 3,
        rowNum: 3,
      });
    } else if (mode === "16") {
      this.setData({
        columnNum: 4,
        rowNum: 4,
      });
    } else if (mode == "32") {
      this.setData({
        columnNum: 4,
        rowNum: 8,
      });
    }
  },

  /**
   * 添加屏幕
   */
  addScreenTapHandler() { },

  onDeviceItemTap(e) {
    const id = e.currentTarget.dataset.id;
    this.setData({
      selectedDeviceId: id,
    });
  },

  /**
   * 扫码
   */
  onScanTap() {
    wx.scanCode({
      success: (res) => {
        this.setData({
          screenSN: res.result,
        });
      },
    });
  },

  /**
   * 模板长按事件
   */
  templateLongTap() {
    this.setData({
      actionSheetShow: true,
      actionSheetShow2: false,
    });
  },

  onDialogConfirm() {
    this.setData({
      addScreenDialogShow: false,
      actionSheetShow: false,
    });
  },

  onDialogCancel() {
    this.setData({
      actionSheetShow: false,
      addScreenDialogShow: false,
    });
  },

  /**
   * actionSheet 选择事件
   * @param event
   */
  onSelect(event) {
    const index = event.detail.index;
    if (index === 0) {
      // 修改
      this.setData({
        isEdit: true,
        presetsDialogShow: true,
      });
    } else if (index === 1) {
      // 置顶
      console.log("置顶");
    } else if (index === 2) {
      // 删除
      console.log("删除");
    }
  },

  onSelect2(event) {
    const index = event.detail.index;
    if (index === 0) {
      // 修改
      this.setData({
        isEdit: true,
        addScreenDialogShow: true,
      });
    } else if (index === 1) {
      // 添加
      this.setData({
        isEdit: false,
        addScreenDialogShow: true,
      });
    } else if (index === 2) {
      // 删除
      console.log("删除");
    }
  },

  onActionSheetClose() {
    this.setData({
      actionSheetShow: false,
    });
  },

  onActionSheetClose2() {
    this.setData({
      actionSheetShow2: false,
    });
  },

  onActionSheetCancel() {
    this.setData({
      actionSheetShow: false,
    });
  },

  onActionSheetCancel2() {
    this.setData({
      actionSheetShow2: false,
    });
  },

  /**
   * 添加预设模板
   */
  onAddPresettTap() {
    this.setData({
      presetsDialogShow: true,
    });
  },

  onPresetClose() {
    this.setData({
      presetsDialogShow: false,
    });
  },

  savePresets() {
    console.log("保存预设");
    this.setData({
      presetsDialogShow: false,
    });
  },

  onScreenLongTap() {
    this.setData({
      actionSheetShow2: true,
      actionSheetShow: false,
    });
  },
});
